package com.library.utils
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	public class Track
	{
		public static const TRACK_TYPE_LINK:String = "linkTrack";
		public static const TRACK_TYPE_MAIL:String = "mailTrack";
		public static const TRACK_TYPE_PAGE:String = "pageTrack";
		public static const TRACK_TYPE_ADV:String = "adTrack";
		
		// 增值方案
		public static const TRACK_TYPE_INCREMENT:String = "increment";
		
		public static const ADV_TYPE_TICKERTAPE:String = "Ticker Tape";
		public static const ADV_TYPE_TOPBANNER:String = "Top Banner";
		public static const ADV_TYPE_THUMB:String = "Thumb Banner";		// 右边的小广告框
		
		// The URL for responding page
		private var _request:URLRequest;	// http://www.tranck.com/tool/analytic/default.aspx
		private var _loader:URLLoader;
		
		private var _type:String;
		private var _bookName:String;
		private var _value:String;
		
		private var _dataXML:XML;
		
		private var _date:Date;
		private var _strData:String;
		
		public function Track()
		{
			super();
			_date = new Date();
			_strData = _date.getFullYear().toString();
			_strData += format(_date.getMonth()+1)
			_strData += format(_date.getDate());
			
			_loader = new URLLoader();
			_loader.addEventListener(Event.COMPLETE, onComplete);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
		}
		
		private function format(value:uint):String{
			var str:String = value.toString();
			if(str.length == 1){
				str = "0" + str;
			}
			return str;
		}
		
		// public ////////////////////////////////		
		// Track 的初始化函数会在 Ezine 类的 initModel 里面会调用。
		public function init(trackURL:String, bookName:String):void{
			if(trackURL != null || trackURL != ""){
				_request = new URLRequest(trackURL);
			}else{
				_request = new URLRequest("http://www.tranck.com/tool/analytic/default.aspx");
			}
			_request.contentType = "text/xml";
			_request.method = URLRequestMethod.POST;
			
			_bookName = bookName;
		}
		
		public function send(type:String, value:String):void{
			_type = type;
			_value = value;
			
			//var str:String = "<command type='"+_type+"'><data name='bookName'>"+_bookName+"</data><data name='value'>"+_value+"</data></command>";
			
			var str:String = "" + 
					"<command id='addTracking'>" +
					"<time>" + _strData + "</time>" +
					"<name>" + _bookName + "</name>" + 
					"<type>" + _type + "</type>" + 
					"<value><![CDATA[" + _value + "]]></value>" +  
					"</command>";

			_dataXML = new XML(str);
			
			_request.data = _dataXML.toXMLString();
			
			try {
				_loader.load(_request);
			} catch (error:ArgumentError) {
				//Logger.error("An ArgumentError has occurred.");
			} catch (error:SecurityError) {
				//Logger.error("An SecurityError has occurred.");
			}
		}
		
		// Event
		private function onComplete(evt:Event):void {
			//Logger.debug("Tracking action \"" + _type + "\" successful with value of \"" + _value + "\"");
		}
		private function onError(evt:IOErrorEvent):void{
			//Logger.debug("IO Error, can not access the tracking server");
		}
		
		// Singleton
		private static var _instance:Track;
        public static function getInstance() : Track {
            if ( _instance == null ) _instance = new Track( );
            return _instance as Track;
        }

	}
}